{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Embedding a Bokeh server in a Notebook\n",
    "\n",
    "This notebook shows how a Bokeh server application can be embedded inside a Jupyter notebook. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import yaml\n",
    "\n",
    "from bokeh.layouts import column\n",
    "from bokeh.models import ColumnDataSource, Slider\n",
    "from bokeh.plotting import figure\n",
    "from bokeh.themes import Theme\n",
    "from bokeh.io import show, output_notebook\n",
    "\n",
    "from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature\n",
    "\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are various application handlers that can be used to build up Bokeh documents. For example, there is a `ScriptHandler` that uses the code from a `.py` file to produce Bokeh documents. This is the handler that is used when we run `bokeh serve app.py`. In the notebook we can use a function to define a Bokehg application.\n",
    "\n",
    "Here is the function `bkapp(doc)` that defines our app:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bkapp(doc):\n",
    "    df = sea_surface_temperature.copy()\n",
    "    source = ColumnDataSource(data=df)\n",
    "\n",
    "    plot = figure(x_axis_type='datetime', y_range=(0, 25),\n",
    "                  y_axis_label='Temperature (Celsius)',\n",
    "                  title=\"Sea Surface Temperature at 43.18, -70.43\")\n",
    "    plot.line('time', 'temperature', source=source)\n",
    "\n",
    "    def callback(attr, old, new):\n",
    "        if new == 0:\n",
    "            data = df\n",
    "        else:\n",
    "            data = df.rolling('{0}D'.format(new)).mean()\n",
    "        source.data = ColumnDataSource.from_df(data)\n",
    "\n",
    "    slider = Slider(start=0, end=30, value=0, step=1, title=\"Smoothing by N Days\")\n",
    "    slider.on_change('value', callback)\n",
    "\n",
    "    doc.add_root(column(slider, plot))\n",
    "\n",
    "    doc.theme = Theme(json=yaml.load(\"\"\"\n",
    "        attrs:\n",
    "            Figure:\n",
    "                background_fill_color: \"#DDDDDD\"\n",
    "                outline_line_color: white\n",
    "                toolbar_location: above\n",
    "                height: 500\n",
    "                width: 800\n",
    "            Grid:\n",
    "                grid_line_dash: [6, 4]\n",
    "                grid_line_color: white\n",
    "    \"\"\", Loader=yaml.FullLoader))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can display our application using ``show``, which will automatically create an ``Application`` that wraps ``bkapp`` using ``FunctionHandler``. The end result is that the Bokeh server will call ``bkapp`` to build new documents for every new sessions that is opened.\n",
    "\n",
    "**Note**: If the current notebook is not displayed at the default URL, you must update the `notebook_url` parameter in the comment below to match, and pass it to `show`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "show(bkapp) # notebook_url=\"http://localhost:8888\" "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
